package com.rockxin.cat.activity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.TagAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.bean.TagBean;
import com.rockxin.cat.event.TargetFilterEvent;
import com.rockxin.cat.utils.FlowTagLayout.FlowTagLayout;
import com.rockxin.cat.utils.FlowTagLayout.OnTagClickListener;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by cat on 18-3-7.
 */

public class TargetFilterActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.flow_tag_layout_profits)
    FlowTagLayout flowTagLayoutProfits;
    @BindView(R.id.flow_tag_layout_term)
    FlowTagLayout flowTagLayoutTerm;
    @BindView(R.id.flow_tag_layout_borrower)
    FlowTagLayout flowTagLayoutBorrower;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.btn_back)
    Button btnBack;

    private TagAdapter mProfitsTagAdapter;
    private TagAdapter mTermTagAdapter;
    private TagAdapter mBorrowerTagAdapter;

    private List<TagBean> profitsList = new ArrayList<>();
    private List<TagBean> termList = new ArrayList<>();
    private List<TagBean> borrowerList = new ArrayList<>();

    private String mProfitsId = "";
    private String mTermId = "";
    private String mBorrowerId = "";

    private TargetFilterEvent mTargetFilterEvent;

    @Override
    protected int setLayout() {
        return R.layout.activity_target_filter;
    }

    @Override
    public void initData() {
        initView();
        initFlowTag();
        setListener();
    }

    public void initView() {
        title.setText("筛选");
        mTargetFilterEvent = new TargetFilterEvent();
    }

    public void initFlowTag() {
        mProfitsTagAdapter = new TagAdapter(this);
        flowTagLayoutProfits.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        flowTagLayoutProfits.setAdapter(mProfitsTagAdapter);
        flowTagLayoutProfits.setOnTagClickListener(new OnTagClickListener() {
            @Override
            public void onItemClick(FlowTagLayout parent, View view, int position) {
                Logger.d(profitsList.get(position).getId().length() + "===:" + profitsList.get(position).getId());
                String flag = profitsList.get(position).getId();
                mProfitsId = mProfitsId.equals(flag) ? "" : flag;
            }
        });

        mTermTagAdapter = new TagAdapter(this);
        flowTagLayoutTerm.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        flowTagLayoutTerm.setAdapter(mTermTagAdapter);
        flowTagLayoutTerm.setOnTagClickListener(new OnTagClickListener() {
            @Override
            public void onItemClick(FlowTagLayout parent, View view, int position) {
                Logger.d(termList.get(position).getId().length() + "===:" + termList.get(position).getId());
                String flag = termList.get(position).getId();
                mTermId = mProfitsId.equals(flag) ? "" : flag;
            }
        });

        mBorrowerTagAdapter = new TagAdapter(this);
        flowTagLayoutBorrower.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        flowTagLayoutBorrower.setAdapter(mBorrowerTagAdapter);
        flowTagLayoutBorrower.setOnTagClickListener(new OnTagClickListener() {
            @Override
            public void onItemClick(FlowTagLayout parent, View view, int position) {
                Logger.d(borrowerList.get(position).getId().length() + "===:" + borrowerList.get(position).getId());
                String flag = borrowerList.get(position).getId();
                mBorrowerId = mBorrowerId.equals(flag) ? "" : flag;
            }
        });

        initTagData();
    }

    public void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTargetFilterEvent.setmProfitsId(mProfitsId);
                mTargetFilterEvent.setmTermId(mTermId);
                mTargetFilterEvent.setmBorrowerId(mBorrowerId);
                EventBus.getDefault().post(mTargetFilterEvent, "target_filter");
                finish();
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void initTagData() {
        profitsList.add(new TagBean("不限", ""));
        profitsList.add(new TagBean("10%以上", "10"));
        profitsList.add(new TagBean("12%以上", "12"));
        profitsList.add(new TagBean("15%以上", "15"));
        profitsList.add(new TagBean("18%以上", "18"));
        mProfitsTagAdapter.onlyAddAll(profitsList);

        termList.add(new TagBean("不限", ""));
        termList.add(new TagBean("1天以内", "1"));
        termList.add(new TagBean("3天以内", "3"));
        termList.add(new TagBean("6天以内", "6"));
        termList.add(new TagBean("9天以内", "9"));
        termList.add(new TagBean("12天以内", "12"));
        mTermTagAdapter.onlyAddAll(termList);

        borrowerList.add(new TagBean("不限", ""));
        borrowerList.add(new TagBean("个人借款", "1"));
        borrowerList.add(new TagBean("企业借款", "2"));
        mBorrowerTagAdapter.onlyAddAll(borrowerList);
    }

}
